﻿Imports System.Activities
Imports System.ComponentModel
Imports System.Activities.Presentation.PropertyEditing
Imports System.Collections.ObjectModel
Imports System.Activities.Presentation.Metadata
Imports System.Drawing

Public Class FolderCreateActivity
    Inherits AsyncCodeActivity
    Implements IRegisterMetadata

    Public Property FolderName As InArgument(Of String)
    Public Property TargetFolder As InArgument(Of String)
    Public Property Result As OutArgument(Of Boolean)

    Private Delegate Function asyncCreateFolderDelegate(ByVal target As String, ByVal name As String) As Boolean

    Public Sub New()
        Me.DisplayName = My.Resources.FolderCreateDisplayName
    End Sub

    Protected Overrides Function BeginExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal callback As System.AsyncCallback, ByVal state As Object) As System.IAsyncResult
        Dim target = context.GetValue(Me.TargetFolder)
        Dim name = context.GetValue(Me.FolderName)
        Dim asyncExecute = New asyncCreateFolderDelegate(AddressOf CreateFolder)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(target, name, callback, state)
    End Function

    Protected Overrides Sub EndExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal result As System.IAsyncResult)
        Dim asyncExecute = TryCast(context.UserState, asyncCreateFolderDelegate)
        Dim getResult = asyncExecute.EndInvoke(result)

        context.SetValue(Me.Result, getResult)
    End Sub

    Private Function CreateFolder(ByVal target As String, ByVal name As String) As Boolean

        Dim result As Boolean = False
        Try
            target = System.IO.Path.Combine(target, name)
            If IO.Directory.Exists(target) Then Exit Try
            My.Computer.FileSystem.CreateDirectory(target)
            result = True
        Catch ex As Exception

        End Try
        Return result
    End Function

    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder
        'FolderCreate Activity
        build.AddCustomAttributes(GetType(FolderCreateActivity),
                                              New ToolboxBitmapAttribute(GetType(FolderCreateActivity), "FolderCreate.png"))
        build.AddCustomAttributes(GetType(FolderCreateActivity), "FolderName",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FolderpathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FolderCreateCategory),
                                              New DisplayNameAttribute(My.Resources.FolderCreateFolderNameDisplay),
                                              New DescriptionAttribute(My.Resources.FolderCreateFolderNameDescription))
        build.AddCustomAttributes(GetType(FolderCreateActivity), "TargetFolder",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FolderpathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FolderCreateCategory),
                                              New DisplayNameAttribute(My.Resources.FolderCreateTargetFolderDisplay),
                                              New DescriptionAttribute(My.Resources.FolderCreateTargetFolderDescription))
        build.AddCustomAttributes(GetType(FolderCreateActivity), "Result",
                                              New CategoryAttribute(My.Resources.FolderCreateCategory),
                                              New DisplayNameAttribute(My.Resources.FolderCreateResultDisplay),
                                              New DescriptionAttribute(My.Resources.FolderCreateResultDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub

End Class
